import { createRouter, createWebHistory } from 'vue-router'
export const routes=[
  {
    path: '/',
    name: 'home',
    meta: { requiresAuth: true ,title:'系统首页'},
    redirect: '/home',
    component: () => import('@/layout/MyLayout.vue'),
    children: [
      {
        path: '/home',
        meta: { requiresAuth: true },
        component: () => import('@/views/home/index.vue')
      }
    ]
  },
  {
    path: '/banner',
    name: 'banner',
    meta: { requiresAuth: true ,title:'轮播图管理'},
    redirect: '/banner/list',
    component: () => import('@/layout/MyLayout.vue'),
    children: [
      {
      path: '/banner/list',
      redirect: '/banner/list/home',
      meta: { requiresAuth: true ,title:'轮播图列表'},
      component: () => import('@/views/banner/index.vue'),
      children: [
        { path: '/banner/list/home', 
          meta: { requiresAuth: true ,title:'轮播图列表',iconName:'Filter'},
          component: () => import('@/views/banner/components/BannerHome.vue') },
        { path: '/banner/list/kind', 
          meta: { requiresAuth: true ,title:'分类轮播图列表',iconName:'Filter'},
          component: () => import('@/views/banner/components/BannerKind.vue') },
          { path: '/banner/add', 
            meta: { requiresAuth: true ,title:'添加轮播图',iconName:'Filter'},
            component: () => import('@/views/banner/add.vue') }
      ]
    }
    ]
  },
  { path: '/login', name: 'login', component: () => import('@/views/login/index.vue') },
  {
    path: '/pro',
    name: 'pro',
    redirect: '/pro/list',
    meta: { requiresAuth: true ,title:'产品管理'},
    component: () => import('@/layout/MyLayout.vue'),
    children: [
      {
        path: '/pro/list',
        meta: { requiresAuth: true ,title:'产品列表',iconName:'Filter'},
        component: () => import('@/views/pro/components/ProList.vue')
      },
      {
        path: '/pro/shaixuan',
        meta: { requiresAuth: true ,title:'筛选列表',iconName:'Filter'},
        component: () => import('@/views/pro/components/ShaiXuanList.vue')
      }
    ]
  },
  {
    path: '/accues',
    name: 'accues',
    redirect: '/accues/adminlist',
    meta: { requiresAuth: true ,title:'账户管理'},
    component: () => import('@/layout/MyLayout.vue'),
    children: [
      {
        path: '/accues/adminlist',
        meta: { requiresAuth: true ,title:'管理员列表',iconName:'Avatar'},
        component: () => import('@/views/accues/components/AdminList.vue')
      },
      { path: '/accues/userlist',
        meta: { requiresAuth: true ,title:'用户列表',iconName:'UserFilled'}, 
        component: () => import('@/views/accues/components/UserList.vue') }
    ]
  }
  
]
const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes
})
import { useUserStore } from '@/stores/useUser';
router.beforeEach((to, from) => {
  const useUser = useUserStore(); 
  // 而不是去检查每条路由记录
  // to.matched.some(record => record.meta.requiresAuth)
  if (to.meta.requiresAuth && !useUser.isLogin) {
    // 此路由需要授权，请检查是否已登录
    // 如果没有，则重定向到登录页面
    return {
      path: '/login',
      // 保存我们所在的位置，以便以后再来
      query: { redirect: to.fullPath },
    }
  }
}) 
export default router
